package com.icinfo.cloud.provider.punish.fastcase.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.punish.evideinv.dto.QueryEvidencePageDto;
import com.icinfo.cloud.provider.punish.evideinv.service.EvidenceInventoryService;
import com.icinfo.cloud.provider.punish.evideinv.vo.EvidencePageNewVo;
import com.icinfo.cloud.provider.punish.fastcase.vo.FastCaseDocumentDataVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStageEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseOfflineDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseOfflineDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseDocumentRecordCodeNameVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.framework.common.web.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Desc 描述
 * @Author mahan
 * @Date 2023/3/9
 */
@RestController
@RequestMapping("/punish/fast/case/document/template")
public class FastCaseDocumentTemplateController {

	@Resource
	private EvidenceInventoryService evidenceInventoryService;
	@Resource
	private ICaseOfflineDocumentRecordService caseOfflineDocumentRecordService;
	@Resource
	private ICaseDocumentRecordService documentRecordService;
	@Resource
	private ICaseMainInfoService caseMainInfoService;



	/**
	 * 查询调查终结报告中的相关证据：
	 * 1、询问笔录、现场检查记录表（这个两个文书必须有，所以不用单独查询）
	 * 2、线下证据
	 * 3、线下文书
	 * 4、调查取证阶段除了“案件调查终结报告”之外，所有生成的文书(包含了询问笔录、现场检查记录表)
	 * 结果：名称以逗号隔开
	 * @param caseId
	 * @return
	 */
	@GetMapping("/getEvidenceNames")
	public Result<FastCaseDocumentDataVo> getEvidenceNames(@RequestParam String caseId){
		CaseMainInfoVo caseMainInfoVo = caseMainInfoService.getCaseMainInfo(caseId);

		List<String> reList = CollUtil.newArrayList();
		//获取线下证据
		QueryEvidencePageDto evidenceQueryDto = new QueryEvidencePageDto();
		evidenceQueryDto.setCaseId(caseId);
		evidenceQueryDto.setPageSize(1000);
		evidenceQueryDto.setPageNum(1);
		PageResponse<EvidencePageNewVo> page =  evidenceInventoryService.getEvidencePage(evidenceQueryDto);
		if(ObjectUtil.isNotEmpty(page.getData())){
			page.getData().forEach(u -> reList.add(u.getEvidenceName()));
		}
		//获取线下文书
		List<CaseOfflineDocumentRecord> offlineDocumentRecords = caseOfflineDocumentRecordService.getCaseOfflineDocumentRecord(caseId, CaseStageEnum.INVESTIGATION.getCode());
		if(ObjectUtil.isNotEmpty(offlineDocumentRecords)){
			offlineDocumentRecords.forEach(u -> reList.add(u.getDocumentTittle()));
		}
		//获取调查取证阶段除了“案件调查终结报告”之外，所有生成的文书(包含了询问笔录、现场检查记录表)
		List<CaseDocumentRecordCodeNameVo> records = documentRecordService.getCaseDocumentRecordByStageCode(caseId, CaseStageEnum.INVESTIGATION.getCode(), caseMainInfoVo.getBusiParamId());
		if(ObjectUtil.isNotEmpty(records)){
			records.forEach(u -> reList.add(u.getDocumentCatalogName()));
		}
		FastCaseDocumentDataVo reVo = new FastCaseDocumentDataVo();
		reVo.setEvidenceNames(CollUtil.join(reList, ","));
		return Result.success(reVo);
	}
}
